package com.wyg.epkafkastarter.encoder;

import com.wyg.epkafkastarter.msg.MailMsg;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: WangYongGan
 * @Date: 2022/5/18 1:57
 * @Description:
 */
@Slf4j
public class MailEncoder {

    public static String encode(MailMsg msg) {
        StringBuilder sb = new StringBuilder(msg.getTo());
        sb.append(":::");
        for (String entity : msg.getEntities()) {
            sb.append(entity);
            sb.append(",");
        }
        return sb.toString();
    }

    public static MailMsg decode(String encode) {
        try {
            final String[] split = encode.split(":::");
            String to = split[0];
            final List<String> entities = Arrays.asList(split[1].split(","));
            return MailMsg.builder().to(to).entities(entities).build();
        } catch (Exception ignored) {}
        return null;
    }
}
